package backingBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import model.Product;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import abstractFactory.Factory;
import dao.Product.ProductDAO;

public class BeanProduct {
	private Product product = new Product();
	private BeanCategory bbcat;

	public BeanProduct() {
		getAllProducts();
	}

	public void register(){
		ProductDAO productDao = Factory.getInstance("JPA").createProductDAO();
		if(productDao.save(product)){
			FacesMessage msg = new FacesMessage("Cadastrado com Sucesso.");
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.addMessage("gravar", msg);
		}	
		else{
			FacesMessage msg = new FacesMessage("Tente outra vez.");
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.addMessage("gravar", msg);

		}	
		product = new Product();
	}

	public List<Product> getProducts(){
		if(bbcat.getCategory().getId() == 0) {
			return getAllProducts();
		}
		else 
			return getProductsByCategory();
	}

	public List<Product> getAllProducts(){
		ProductDAO productDao = Factory.getInstance("JPA").createProductDAO();
		return productDao.getAllProducts();
	}

	public List<Product> getProductsByCategory(){
		ProductDAO productDao = Factory.getInstance("JPA").createProductDAO();
		return productDao.getProductsByCategory(bbcat.getCategory().getId());

	}

	public Product editProduct(){
		return product;
	}

	public void delete(){
		ProductDAO productDao = Factory.getInstance("JPA").createProductDAO();
		product.setActive(0);
		productDao.update(product);
		product = new Product();
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public BeanCategory getBbcat() {
		return bbcat;
	}

	public void setBbcat(BeanCategory bbcat) {
		this.bbcat = bbcat;
	}





	//FILE UPLOAD



	private ArrayList<File> files = new ArrayList<File>();
	private int uploadsAvailable = 3;
	private boolean autoUpload = false;
	private boolean useFlash = false;
	public int getSize() {
		if (getFiles().size()>0){
			return getFiles().size();
		}else 
		{
			return 0;
		}
	}

//	public void paint(OutputStream stream, Object object) throws IOException {
//		stream.write(getFiles().get((Integer)object).getData());
//	}

	public void listener(UploadEvent event) throws Exception{
		FacesContext fc = FacesContext.getCurrentInstance();
        ServletContext sc = (ServletContext)fc.getExternalContext().getContext();
        String dir = sc.getRealPath("/")+"ProductImage"+File.separator;
        File archive = new File(dir+event.getUploadItem().getFileName());
        UploadItem item = event.getUploadItem();
        item.getFile().renameTo(archive);
        files.add(archive);
	}  
	public void getListener(UploadEvent event) throws Exception{
		FacesContext fc = FacesContext.getCurrentInstance();
        ServletContext sc = (ServletContext)fc.getExternalContext().getContext();
        String dir = sc.getRealPath("/")+"ProductImage"+File.separator;
        File archive = new File(dir+event.getUploadItem().getFileName());
        UploadItem item = event.getUploadItem();
        item.getFile().renameTo(archive);
        files.add(archive);
	}

	public String clearUploadData() {
		files.clear();
		setUploadsAvailable(3);
		return null;
	}

	public long getTimeStamp(){
		return System.currentTimeMillis();
	}

	public ArrayList<File> getFiles() {
		return files;
	}

	public void setFiles(ArrayList<File> files) { 
		this.files = files;
	}

	public int getUploadsAvailable() {
		return uploadsAvailable;
	}

	public void setUploadsAvailable(int uploadsAvailable) {
		this.uploadsAvailable = uploadsAvailable;
	}

	public boolean isAutoUpload() {
		return autoUpload;
	}

	public void setAutoUpload(boolean autoUpload) {
		this.autoUpload = autoUpload;
	}

	public boolean isUseFlash() {
		return useFlash;
	}

	public void setUseFlash(boolean useFlash) {
		this.useFlash = useFlash;
	}
}